package com.sojson.project.sys.notice.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.project.sys.notice.controller.param.verify.NoticeParamVerify;
import com.sojson.project.sys.notice.entity.dto.NoticeDto;
import com.sojson.project.sys.notice.entity.dto.NoticePageDto;
import com.sojson.project.sys.notice.entity.po.Notice;
import com.sojson.project.sys.notice.entity.vo.NoticeVo;
import com.sojson.project.sys.notice.service.NoticeService;
import com.sojson.result.Result;
import com.sojson.util.poi.ExcelUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 公告表控制器
 * 
 * @author liu
 * @date 2022-10-24
 */
@Api(tags = "公告表管理")
@RestController
@RequestMapping("/sys/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 单条插入
     * 
     * @param dto
     * @return
     */
    @PostMapping("/save")
    @PreAuthorize("@ss.hasPermi('/sys/notice/save')")
    @ApiOperation("给公告表添加一条数据")
    public Result<Boolean> save(@RequestBody Notice dto) {
        // 校验参数
        Result<Boolean> paramResult = NoticeParamVerify.save(dto);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        return Result.success(noticeService.save(dto));
    }

    /**
     * 通过id更新
     * 
     * @param dto
     * @return
     */
    @PutMapping("/updateById")
    @PreAuthorize("@ss.hasPermi('/sys/notice/updateById')")
    @ApiOperation("通过id更新公告表数据")
    public Result<Boolean> updateById(@RequestBody Notice dto) {
        // 校验参数
        Result<Boolean> paramResult = NoticeParamVerify.updateById(dto);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        return Result.success(noticeService.updateById(dto));
    }

    /**
     * 列表查询
     * 
     * @param dto
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('/sys/notice/list')")
    @ApiOperation(value = "查询公告表数据", response = NoticeVo.class)
    public Result<List<NoticeVo>> list(NoticeDto dto) {
        return Result.success(noticeService.list(dto));
    }

    /**
     * 分页查询
     * 
     * @param dto
     * @return
     */
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermi('/sys/notice/page')")
    @ApiOperation(value = "分页查询公告表数据", response = NoticeVo.class)
    public Result<List<NoticeVo>> page(NoticePageDto dto) {
        return Result.success(noticeService.page(dto));
    }

    /**
     * 导出数据
     * 
     * @param dto
     * @return
     * @throws Exception 
     */
    @GetMapping("/export")
    @PreAuthorize("@ss.hasPermi('/sys/notice/export')")
    @ApiOperation("导出公告表数据")
    public void export(NoticeDto dto) throws Exception {
        List<NoticeVo> list = noticeService.list(dto);
        ExcelUtil<NoticeVo> excelUtil = new ExcelUtil<>(NoticeVo.class);
        excelUtil.exportExcelOutFile(list, "公告表");
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("@ss.hasPermi('/sys/notice/info/{id}')")
    @ApiOperation(value = "通过id查询公告表数据", response = NoticeVo.class)
    public Result<NoticeVo> info(@PathVariable String id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(noticeService.findById(id));
    }

    /**
     * 通过id列表删除
     * 
     * @param dto
     * @return
     */
    @DeleteMapping("/remove")
    @PreAuthorize("@ss.hasPermi('/sys/notice/remove')")
    @ApiOperation("通过id列表删除公告表数据")
    public Result<Boolean> remove(@RequestBody NoticeDto dto) {
        List<String> ids = dto.getIds();
        if (ids == null || ids.size() == 0) {
            return Result.success();
        }

        return Result.success(noticeService.removeByIds(ids));
    }

    /**
     * 清空数据
     * 
     * @return
     */
    @DeleteMapping("/clear")
    @PreAuthorize("@ss.hasPermi('/sys/notice/clear')")
    @ApiOperation("清空公告表数据")
    public Result<Boolean> clear() {
        return Result.success(noticeService.clear());
    }

    /**
     * 通过id删除
     * 
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("@ss.hasPermi('/sys/notice/remove/{id}')")
    @ApiOperation("通过id删除公告表数据")
    public Result<Boolean> remove(@PathVariable String id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(noticeService.removeById(id));
    }

}